// pages/comment/comment.js
import request from '../../utils/request'



Page({

  /**
   * 页面的初始数据
   */
  data: {
    hotComments:[],//评论列表
    musicid:'',//音乐id
    liked:false,//是否喜欢
    typecountitem:1,//评论类型
    showfloor:false,//显示隐藏楼层
    hotCommentscommentId: '',//点击回复的楼主id
    commentsfloor:'',//楼层信息
    showfloorindex:'',//楼层楼主在原数组的索引
    hotCommentsfloor:'',//楼主信息
    floorCommentTime:0,//楼层评论最后一项的时间
    commentstime:0,//评论列表最后一个的时间
    pageno:1,//标记页数
    controlbindscrolltolower: true,//控制评论列表滑动到底部触发
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.musicid)
    // options.musicid && this.getcomments(options.musicid)
    if (options.musicid){
      this.setData({
        musicid: options.musicid
      })
      // this.getcomments(options.musicid)
      this.gettype();

    }
  },
  //修改评论类型
  handtypecount(event){
   
    let typecountitem = event.currentTarget.dataset.typecount
    let { pageno} = this.data
    console.log(typecountitem)
    this.setData({
      typecountitem,
      pageno:1
    })
    this.gettype()
  },
  //根据类型获取评论
 async gettype(){
   let { musicid, typecountitem, hotComments, commentstime} = this.data

   let res = await request('/comment/new', { type: 0, id: musicid, sortType: typecountitem })
   console.log(res.data.comments)
     hotComments = res.data.comments  
     //在数组最后一个时间改变前存储时间
   commentstime = hotComments[hotComments.length - 1].time
   this.setData({
     commentstime
   })
     //修改数组每个time属性时间格式
      hotComments.forEach(item => {
        // console.log(item.time)
        //毫秒转化年月日
     item.time = this.getymd(item.time)
        // console.log(item.time)
      })
  // console.log(hotComments)
   this.setData({
     hotComments
   })
  },

  //评论列表滑动到底部触发
async getcommentsaddlist(){
  let { controlbindscrolltolower} = this.data
if (controlbindscrolltolower){
  this.setData({
    controlbindscrolltolower:false
  })
  }else{
    return
  }
    let { musicid, typecountitem, hotComments, commentstime,pageno } = this.data

  // console.log(hotComments)
  if (commentstime == 0) {
    wx.showToast({
      title: '已经到底了亲',
    })
    return
  }
  pageno = pageno + 1
  this.setData({
    pageno
  })

    // sortType为3时且页数不是第一页时需传入, 值为上一条数据的time
  let result = '';
   if (typecountitem == 3 && pageno != 1){
     result = await request('/comment/new', { type: 0, id: musicid, sortType: typecountitem, cursor: commentstime, pageNo: pageno })
   }else{
     result = await request('/comment/new', { type: 0, id: musicid, sortType: typecountitem, pageNo: pageno  })
   }
 
  console.log(result)
  this.setData({
    controlbindscrolltolower:true
  })
  if (result.data.comments.length == 0) {
    this.setData({
      commentstime: 0
    })

    return
  }
  result = result.data.comments;
  // 在处理时间前先保存
  commentstime = result[result.length - 1].time;


  //修改数组每个time属性时间格式
  result.forEach(item => {
    // console.log(item.time)
    //毫秒转化年月日
    item.time = this.getymd(item.time)
    // console.log(item.time)
  })
  //将修改后的数据添加进评论
  hotComments.push(...result)
  console.log(commentstime)
  this.setData({
    hotComments,
    commentstime
  })




  },

  //获取评论
//  async getcomments(musicid){
//    let { hotComments} = this.data
  
//   let res = await request('/comment/music', { id: musicid,limit:1})
//   console.log(res)
//    hotComments = res.hotComments
  
//    console.log(hotComments)
//    this.setData({
//      hotComments
//    })
//   },

  //点赞
 async handlike(event){
   //点的索引
  let handindex = event.currentTarget.dataset.handindex
  //  console.log(handindex)
  //  console.log(event)
  
   let { musicid, hotComments, commentsfloor} = this.data

   //存储当前索引的数组
  //  let arrlod = hotComments[handindex]
   
   console.log(hotComments)
   let commentId = hotComments[handindex].commentId 

   let liked = hotComments[handindex].liked
  //  console.log(liked)
   let likedCount = hotComments[handindex].likedCount
  //  console.log(likedCount)
  //  console.log(hotComments[handindex])
  let count = 0;
   if (!liked){
    count=1
     likedCount = likedCount + 1
   
  }else{
    count=0
     likedCount = likedCount - 1
  }


   hotComments[handindex].liked = !liked;
   hotComments[handindex].likedCount=likedCount;
   console.log(hotComments)


 
  //存储当前索引的数组修改后的数据
  //  let arrnew = hotComments[handindex];

   let res = await request('/comment/like', { id: musicid, cid: commentId, t: count, type:0})
   console.log(res.code)
   if (res.code){
     //替换数组中当前索引的值
    //  console.log(hotComments)
    //  hotComments.splice(handindex, 1, arrnew);
    //  console.log(hotComments)
     this.setData({
       hotComments,
     })
   }
  },
  //点击切换楼层的显示隐藏
  handfloor(){
    let { showfloor} = this.data
    this.setData({
      showfloor: !showfloor
    })
  },
  //显示楼层
 async showfloor(e){
   console.log(e)
   let index = e.currentTarget.dataset.index
  
   console.log(index)

  
   let { showfloor, hotComments, hotCommentsfloor, hotCommentscommentId, musicid, commentsfloor, showfloorindex, floorCommentTime} = this.data;
   showfloorindex = index
   hotCommentscommentId = hotComments[index].commentId
  //  hotCommentsfloor = hotComments[index]
    this.setData({
      showfloor: !showfloor,
      hotCommentsfloor,
      hotCommentscommentId,
      showfloorindex
    })
  
    //获取楼层数据
  let res = await request('/comment/floor', { parentCommentId: hotCommentscommentId, id: musicid, type:0})
  console.log(res)
   commentsfloor = res.data

   if (commentsfloor.comments.length == 20) {
     // 在处理时间前先保存,在下面滑动楼层更新数据用到
     floorCommentTime = commentsfloor.comments[commentsfloor.comments.length - 1].time;
     console.log(floorCommentTime)
     this.setData({
       floorCommentTime
     })
   }

   //修改数组每个time属性时间格式
   commentsfloor.comments.forEach(item => {
     // console.log(item.time)
     //毫秒转化年月日
     item.time = this.getymd(item.time)
     // console.log(item.time)
   })
   commentsfloor.ownerComment.time = this.getymd(commentsfloor.ownerComment.time)

   this.setData({
     commentsfloor
   })
  },
  //更新楼主的点赞
 async handfloorlike(event){
    //点的索引
  let handindex = event.currentTarget.dataset.showfloorindex
    let { musicid, hotComments,commentsfloor } = this.data
   let commentId = commentsfloor.ownerComment.commentId
    let liked = commentsfloor.ownerComment.liked
    let likedCount = commentsfloor.ownerComment.likedCount
   let count = 0;
    if (!liked) {
     count = 1;
      likedCount = likedCount + 1

    } else {
      count = 0
      likedCount = likedCount - 1
    }


    commentsfloor.ownerComment.liked = !liked;
    commentsfloor.ownerComment.likedCount = likedCount;
    hotComments[handindex].liked = !liked;
    hotComments[handindex].likedCount = likedCount;
    let res = await request('/comment/like', { id: musicid, cid: commentId, t: count, type: 0 })
    console.log(res.code)
    if (res.code) {

      this.setData({
        commentsfloor,
        hotComments
      })

    }
   


   
  },
  //楼层点赞
async handfloorliked(event){
    //点的索引
 let floorindex = event.currentTarget.dataset.floorindex
    let { musicid, commentsfloor } = this.data
  //楼层列表的id 点赞数量 是否点赞
    let commentId = commentsfloor.comments[floorindex].commentId
    let liked = commentsfloor.comments[floorindex].liked
    let likedCount = commentsfloor.comments[floorindex].likedCount
    let count = 0;
    if (!liked) {
      count = 1;
      likedCount = likedCount + 1

    } else {
      count = 0
      likedCount = likedCount - 1
    }


    commentsfloor.comments[floorindex].liked = !liked;
    commentsfloor.comments[floorindex].likedCount = likedCount;
 
    let res = await request('/comment/like', { id: musicid, cid: commentId, t: count, type: 0 })
    console.log(res.code)
    if (res.code) {
    
      this.setData({
        commentsfloor
      
      })
    }

  },
  //毫秒转化年月日
  getymd(timer){
    let mydate = new Date(timer)
    let year = mydate.getFullYear()
    let month = mydate.getMonth()+1 //0开始
    let day = mydate.getDate()
    month = this.getaddzero(month)
    day = this.getaddzero(day)
    let objtimer = year+"年"+month+"月"+day+"日"
    return objtimer
  },
  //加0
  getaddzero(num){
  if(parseInt(num)<10){
    num = "0"+num
  }
  return num
  },

  //楼层评论滚动到底层触发
 async getnewfloor(){
   let { controlbindscrolltolower } = this.data
   if (controlbindscrolltolower) {
     this.setData({
       controlbindscrolltolower: false
     })
   } else {
     return
   }
  
   let { floorCommentTime, musicid, hotCommentscommentId, commentsfloor} = this.data
   if (floorCommentTime == 0) {
    wx.showToast({
      title: '已经到底了亲',
    })
     return
   }
   let result = await request('/comment/floor', { parentCommentId: hotCommentscommentId, id: musicid, type: 0, time: floorCommentTime })
    console.log(result)
   this.setData({
     controlbindscrolltolower: true
   })
   if (result.data.comments.length == 0) {
    this.setData({
      floorCommentTime:0
    })
     
     return
   }
   result = result.data.comments;
   // 在处理时间前先保存
   floorCommentTime = result[result.length - 1].time;


   //修改数组每个time属性时间格式
   result.forEach(item => {
     // console.log(item.time)
     //毫秒转化年月日
     item.time = this.getymd(item.time)
     // console.log(item.time)
   })
    //将修改后的数据添加进评论楼层
   commentsfloor.comments.push(...result)
   console.log(floorCommentTime)
   this.setData({
     commentsfloor,
     floorCommentTime
   })
    
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})